
#include "stdafx.h"
#include "childframetools.h"
#include "plugin\textplugin\textplugin_i.h"

UINT WM_TOOLS_VIEWCREATED = RegisterWindowMessage(_T("WM_TOOLS_CREATEVIEW"));

DWORD WINAPI _threadToolsCreateView(LPVOID lpData)
{
	HWND hParent = (HWND)lpData;

	::OleInitialize(NULL);

	CToolsAdapterCreator AdapterCreator;
	CWebContainer *pWebContainer = new CWebContainer(&AdapterCreator);
	CClientRect rcClient(hParent);

	pWebContainer->Create(hParent, rcClient);	
	pWebContainer->CreateWebControl();

	::PostMessage(hParent, WM_TOOLS_VIEWCREATED, 0, (LPARAM)pWebContainer->m_hWnd);

	MSG msg;
	while (::GetMessage(&msg, NULL, 0, 0))
	{
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}

	::OleUninitialize();

	return 0;
}


//////////////////////////////////////////////////////////////////////////
// CToolsExternalDispatchAdapter

// #define EXTERNAL_LOAD_FAVORITES		L"LoadFavorites"
// #define EXTERNAL_SAVE_FAVORITES		L"SaveFavorites"
// 
// #define DISPID_LOAD_FAVORITES		10001
// #define DISPID_SAVE_FAVORITES		10002

CToolsExternalDispatchAdapter::CToolsExternalDispatchAdapter(CWebContainer* pWebContainer)
: CExternalDispatch(pWebContainer)
{
}

CToolsExternalDispatchAdapter::~CToolsExternalDispatchAdapter()
{
}

STDMETHODIMP CToolsExternalDispatchAdapter::GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
	HRESULT hr = NOERROR;
/*
	for (UINT i = 0; i < cNames; ++i)
	{
		if (CSTR_EQUAL == CompareString(lcid, NORM_IGNORECASE | NORM_IGNOREWIDTH, EXTERNAL_LOAD_FAVORITES, -1, (TCHAR *)rgszNames[i], -1))
		{
			rgDispId[i] = DISPID_LOAD_FAVORITES;
		}
		else if (CSTR_EQUAL == CompareString(lcid, NORM_IGNORECASE | NORM_IGNOREWIDTH, EXTERNAL_SAVE_FAVORITES, -1, (TCHAR *)rgszNames[i], -1))
		{
			rgDispId[i] = DISPID_SAVE_FAVORITES;
		}
		else
		{
			hr = ResultFromScode(DISP_E_UNKNOWNNAME);
			rgDispId[i] = DISPID_UNKNOWN;
		}
	}
*/
	return hr;
}

STDMETHODIMP CToolsExternalDispatchAdapter::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
	USES_CONVERSION_EX;
/*
	if (dispIdMember == DISPID_LOAD_FAVORITES && (wFlags & DISPATCH_METHOD))
	{
		CString strBooksInfo;
 		bookns::CBookFavorite bf;
		bf.Load(_Module.GetAppDataPath(_T("favorite.dat")), strBooksInfo);

		if (pVarResult != NULL)
		{
			pVarResult->vt = VT_BSTR;
			pVarResult->bstrVal = ::SysAllocString(T2COLE_EX(strBooksInfo, 0));
		}

		return S_OK ;
	}
	else if (dispIdMember == DISPID_SAVE_FAVORITES && (wFlags & DISPATCH_METHOD))
	{
		bookns::CBookFavorite bf;
		bf.Save(_Module.GetAppDataPath(_T("favorite.dat")), 
			OLE2T_EX(pDispParams->rgvarg[0].bstrVal, 0));

		::PostMessage(GetAncestor(m_pWebContainer->m_hWnd, GA_ROOTOWNER), WM_DATA_NOTIFY, 0, 0);

		return S_OK;
	}
*/
	return E_NOTIMPL ; 
}
//////////////////////////////////////////////////////////////////////////
// CCustomSiteMainFrmAdapter

CToolsCustomSiteAdapter::CToolsCustomSiteAdapter(CWebContainer* pWebContainer)
: CCustomSite(pWebContainer)
{
	m_pExternalDispatch = new CToolsExternalDispatchAdapter(pWebContainer);
	m_pExternalDispatch->AddRef();
}

CToolsCustomSiteAdapter::~CToolsCustomSiteAdapter()
{
	if (m_pExternalDispatch != NULL)
	{
		m_pExternalDispatch->Release();
		m_pExternalDispatch = NULL;
	}
}

STDMETHODIMP CToolsCustomSiteAdapter::GetExternal(IDispatch **ppDispatch)
{
	if (m_pExternalDispatch)
	{
		m_pExternalDispatch->AddRef() ;
		*ppDispatch = m_pExternalDispatch;

		return S_OK ;
	}

	return E_NOTIMPL ;
}

STDMETHODIMP CToolsCustomSiteAdapter::QueryService(REFGUID guidService, REFIID riid, void **ppvObject)
{
	if (!ppvObject)
		return E_INVALIDARG;

	*ppvObject = NULL;

	return E_NOINTERFACE;
}

STDMETHODIMP CToolsCustomSiteAdapter::ShowMessage(HWND hwnd, LPOLESTR lpstrText, LPOLESTR lpstrCaption, DWORD dwType, LPOLESTR lpstrHelpFile, DWORD dwHelpContext, LRESULT *plResult)
{
	USES_CONVERSION_EX;
	CResString<MAX_PATH> strTitle(IDS_APP_TITLE);
	
	*plResult = ::MessageBox(hwnd, OLE2T_EX(lpstrText, 0), strTitle, dwType);

	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
// CToolsWebEventSinkAdapter

CToolsWebEventSinkAdapter::CToolsWebEventSinkAdapter(CWebContainer* pWebContainer)
: CWebEventSink(pWebContainer)
{
}

CToolsWebEventSinkAdapter::~CToolsWebEventSinkAdapter()
{
}

HRESULT CToolsWebEventSinkAdapter::OnNewWindow2(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
	return S_FALSE;
}

HRESULT CToolsWebEventSinkAdapter::OnNewWindow3(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
/*
	*pDispParams->rgvarg[3].pboolVal = VARIANT_TRUE;

	USES_CONVERSION_EX;
	LPCTSTR lpszUrl = OLE2T_EX(pDispParams->rgvarg[0].bstrVal, 0);

	if (!IsLocalSnbFile(lpszUrl))		
		return S_FALSE;

	//>>>>>>
	CDisplayFrame* pFrame = new CDisplayFrame();
	HWND hFrame = pFrame->Create(GetAncestor(m_pWebContainer->m_hWnd, GA_ROOTOWNER), CRect(0, 0, 600, 480));
	pFrame->ShowWindow(SW_SHOW);
	pFrame->UpdateWindow();
	pFrame->Navigate(lpszUrl);
	//<<<<<<
*/
	return S_OK;
}

HRESULT CToolsWebEventSinkAdapter::OnBeforeNavigate2(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
	USES_CONVERSION_EX;

	CComBSTR bstrUrl = pDispParams->rgvarg[5].pvarVal->bstrVal;
	//CString strUrl = OLE2T_EX(bstrUrl, 0);
	
	if (wcsnicmp(bstrUrl, L"plugin://", wcslen(L"plugin://")) == 0)
	{
		*pDispParams->rgvarg[0].pboolVal = VARIANT_TRUE;

		//CString strClsid = strUrl.Mid(_tcslen(_T("plugin://")));
		CLSID clsid;
		if (NOERROR != CLSIDFromString((LPOLESTR)bstrUrl + wcslen(L"plugin://"), &clsid))
		{
			CComPtr<IConvertPlugin> spPlugin;
			if (SUCCEEDED(CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, __uuidof(IConvertPlugin), (LPVOID*)&spPlugin)))
			{
				spPlugin->ShowDialog();
			}
		}
	}
	

	return S_OK;
}

/*
BOOL CToolsWebEventSinkAdapter::IsToppestPage(IDispatch* pDispatch)
{
	if (!pDispatch || !m_pWebBrowser2)
		return FALSE;

	CComPtr<IUnknown> pUnk;
	CComPtr<IDispatch> pWBDisp;

	if (FAILED(m_pWebBrowser2->QueryInterface(IID_IUnknown, (void**)&pUnk)) || !pUnk)
		return FALSE;

	if (FAILED(pUnk->QueryInterface(IID_IDispatch, (void**)&pWBDisp)) || !pWBDisp)
		return FALSE;

	if (pDispatch == pWBDisp)
		return TRUE;

	return FALSE;
}
*/

BOOL CToolsWebEventSinkAdapter::IsLocalSnbFile(LPCTSTR lpszUrl)
{
	CString strUrl = lpszUrl;

	TCHAR szScheme[] = _T("snbfile://");

	if (strUrl.GetLength() < 14)
		return false;

	if (strUrl.Mid(0, 10).CompareNoCase(szScheme) != 0)
		return false;

	CPath path(strUrl.Mid(8));
	if (path.GetExtension().CompareNoCase(_T(".snb")) != 0)
		return false;

	return true;
}

///////////////////////////////////////////////////////////////////////////
// CToolsWebControlExAdapter

CToolsWebControlExAdapter::CToolsWebControlExAdapter(CWebContainer* pWebContainer)
: CWebControlEx(pWebContainer)
{
}

CToolsWebControlExAdapter::~CToolsWebControlExAdapter()
{
}
